package com.leesche.zcsp.node.web.service.impl.data.aop.redis;

import com.leesche.zcsp.node.cache.JedisUtil;
import com.leesche.zcsp.node.web.model.PubDiscount;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 套餐表操作切面拦截器
 * @author reid
 * @Created 16/12/13
 */
@Aspect
@Component
public class DiscountServiceAspect {
    public static final Logger LOGGER = Logger.getLogger(DiscountServiceAspect.class);

    @Pointcut("target(com.leesche.zcsp.node.web.service.PubDiscountService) && (execution(* insert*(..)) || execution(* update*(..)) || execution(* delete*(..)))")
    public void discountAspect() {
        LOGGER.info("套餐表数据库操作切入点......");
    }

    @AfterReturning(pointcut = "discountAspect()", returning = "retValue")
    public void doAfter(JoinPoint joinPoint, Object retValue) {
        String method = joinPoint.getSignature().getName();
        int ret = Integer.parseInt(retValue.toString());

        if (ret > 0) {
            if (method.startsWith("delete")) {
                LOGGER.info("拦截到套餐表删除操作,开始更新redis 缓存......");
                String param = joinPoint.getArgs()[0].toString();//discountId
                JedisUtil.hdel("discountMap", param);
            } else {
                if (joinPoint.getArgs()[0] instanceof PubDiscount) {
                    LOGGER.info("拦截到套餐表增加/更新操作,开始更新redis 缓存......");
                    PubDiscount discount = (PubDiscount) joinPoint.getArgs()[0];
                    if (discount != null && discount.getDiscountid() != null) {
                        JedisUtil.hset("discountMap", String.valueOf(discount.getDiscountid()), discount);
                    }
                }
            }
        }
    }

}
